<%@ page contentType="text/html" %>
<%@ taglib prefix="jbfilter" uri="http://jbfilter.org/tag" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:useBean id="hibernateSessionFactoryRetriever" class="org.jbfilter.demo.HibernateSessionFactoryRetrieverBean" scope="application"/>
<c:set var="datePattern" value="dd/MM/yyyy"/>
<c:set var="checked">checked="checked"</c:set>
<c:set var="selected">selected="selected"</c:set>
<c:set var="countryClass" value="org.jbfilter.test.beans.Country"/>

<html>
<head>
<title>DynamicLogicFilterComponent</title>
</head>
<body>

<jbfilter:filter id="myDynamicLogicHib" varComponents="comps" providerName="hibernate" clean="${param.clean}">
	
	<jbfilter:dynamicLogicComponent
		id="dynComp"
		var="dynComp"
		logic="${param.logic}"
		inverted="${param.inverted}"
		enabled="${!param.disabled}">
		
			<jbfilter:dateComponent
				id="dateComp"
				var="dateComp"
				propertyPath="born"
				value="${param.dateValue}"
				pattern="${datePattern}"
				inverted="${param.dateInverted}"
				enabled="${!param.dateDisabled}"
				comparatorKeyword="${param.dateComparatorKeyword}"
				truncate="DAY"/>
			
			<jbfilter:containsComponent
				id="containsComp"
				var="containsComp"
				propertyPath="lastName"
				value="${param.containsValue}"
				caseSensitive="${param.containsCaseSensitive}"
				inverted="${param.containsInverted}"
				enabled="${!param.containsDisabled}"/>
				
			<jbfilter:equalsComponent
				id="equalsComp"
				var="equalsComp"
				propertyPath="country"
				value="${param.equalsValue}"
				converter="${countryClass}"
				inverted="${param.equalsInverted}"
				enabled="${!param.equalsDisabled}"/>
		
	</jbfilter:dynamicLogicComponent>

</jbfilter:filter>

<form action="">
	<table>
		<tr>
			<td>Born :</td>
			<td colspan="7">&nbsp;</td>
		</tr>
		<tr>
			<td>comparatorKeyword :</td>
			<td>
				<select name="dateComparatorKeyword">
					<option value="gt"<c:if test="${dateComp.comparatorKeyword == 'gt'}"> selected="selected"</c:if>>&gt;</option>
					<option value="ge"<c:if test="${dateComp.comparatorKeyword == 'ge'}"> selected="selected"</c:if>>&gt;=</option>
					<option value="eq"<c:if test="${dateComp.comparatorKeyword == 'eq'}"> selected="selected"</c:if>>=</option>
					<option value="lt"<c:if test="${dateComp.comparatorKeyword == 'lt'}"> selected="selected"</c:if>>&lt;</option>
					<option value="le"<c:if test="${dateComp.comparatorKeyword == 'le'}"> selected="selected"</c:if>>&lt;=</option>
				</select>
			</td>
			<td>value :</td>
			<td><input type="text" name="dateValue" value="<fmt:formatDate value="${dateComp.value}" pattern="${datePattern}"/>"/></td>
			<td>inverted :</td>
			<td><input type="checkbox" name="dateInverted" value="true"<c:if test="${dateComp.inverted}"> ${checked}</c:if>/>inverted</td>
			<td>disabled :</td>
			<td><input type="checkbox" name="dateDisabled" value="true"<c:if test="${!dateComp.enabled}"> ${checked}</c:if>/>disabled</td>
		</tr>
		<tr>
			<td>Lastname :</td>
			<td colspan="7">&nbsp;</td>
		</tr>
		<tr>
			<td>value : </td>
			<td><input type="text" name="containsValue" value="${containsComp.value}"/></td>
			<td>caseSensitive :</td>
			<td><input type="checkbox" name="containsCaseSensitive" value="true"<c:if test="${containsComp.caseSensitive}"> ${checked}</c:if>/>case sensitive</td>
			<td>inverted :</td>
			<td><input type="checkbox" name="containsInverted" value="true"<c:if test="${containsComp.inverted}"> ${checked}</c:if>/>inverted</td>
			<td>disabled :</td>
			<td><input type="checkbox" name="containsDisabled" value="true"<c:if test="${!containsComp.enabled}"> ${checked}</c:if>/>disabled</td>
		</tr>
		<tr>
			<td>Country :</td>
			<td colspan="7">&nbsp;</td>
		</tr>
		<tr>
			<td>value :</td>
			<td>
				<select name="equalsValue">
					<option value="">N/A</option>
					<jbfilter:forEachEnum enumClass="${countryClass}" var="curr">
						<option value="${curr}"<c:if test="${equalsComp.value == curr}">selected="selected"</c:if>>${curr}</option>
					</jbfilter:forEachEnum>
				</select>
			</td>
			<td>inverted :</td>
			<td><input type="checkbox" name="equalsInverted" value="true"<c:if test="${equalsComp.inverted}"> ${checked}</c:if>/>inverted</td>
			<td>disabled :</td>
			<td><input type="checkbox" name="equalsDisabled" value="true"<c:if test="${!equalsComp.enabled}"> ${checked}</c:if>/>disabled</td>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td>
				Logic :
				<select name="logic">
					<jbfilter:forEachEnum enumClass="org.jbfilter.core.fcomps.logic.Logic" var="curr">
						<option value="${curr}"<c:if test="${dynComp.logic == curr}">selected="selected"</c:if>>${curr}</option>
					</jbfilter:forEachEnum>
				</select>
				<input type="submit" value="Ok"/>
				<input type="submit" name="clean" value="Clean"/>
			</td>
			<td colspan="7">&nbsp;</td>
		</tr>
	</table>
</form>

<jbfilter:doFilter
	filterId="myDynamicLogicHib"
	sessionFactory="${hibernateSessionFactoryRetriever.hibernateSessionFactory}"
	className="org.jbfilter.test.beans.Composer"
	varFilteredItems="filteredComposers"/>

<table border="1">
	<thead>
		<tr>
			<td>id</td>
			<td>Name</td>
			<td>born</td>
			<td>died</td>
			<td>country</td>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${filteredComposers}" var="composer">
			<tr>
				<td>${composer.id}</td>
				<td>${composer.name}</td>
				<td><fmt:formatDate value="${composer.born}" pattern="${datePattern}"/></td>
				<td><c:if test="${empty composer.died}">&nbsp;</c:if><fmt:formatDate value="${composer.died}" pattern="${datePattern}"/></td>
				<td>${composer.country}</td>
			</tr>
		</c:forEach>
	</tbody>
</table>
dynComp.inverted : ${dynComp.inverted}<br/>
dynComp.enabled : ${dynComp.enabled}<br/>
dynComp.active : ${dynComp.active}<br/>
dynComp.logic : ${dynComp.logic}<br/>
dateComp.value : ${dateComp.value}<br/>
dateComp.inverted : ${dateComp.inverted}<br/>
dateComp.enabled : ${dateComp.enabled}<br/>
dateComp.active : ${dateComp.active}<br/>
dateComp.value == null : ${dateComp.value == null}<br/>
dateComp.comparatorKeyword : ${dateComp.comparatorKeyword}<br/>
containsComp.value : ${containsComp.value}<br/>
containsComp.caseSensitive : ${containsComp.caseSensitive}<br/>
containsComp.inverted : ${containsComp.inverted}<br/>
containsComp.enabled : ${containsComp.enabled}<br/>
containsComp.active : ${containsComp.active}<br/>
containsComp.value == null : ${containsComp.value == null}<br/>
equalsComp.value : ${equalsComp.value}<br/>
equalsComp.inverted : ${equalsComp.inverted}<br/>
equalsComp.enabled : ${equalsComp.enabled}<br/>
equalsComp.active : ${equalsComp.active}<br/>
equalsComp.value == null : ${equalsComp.value == null}<br/>

${comps.dynComp.filterComponents}

</body>

</html>